package com.sangachy.stg;

import java.util.LinkedList;
import java.util.List;
import java.util.Random;

import com.badlogic.gdx.graphics.g2d.SpriteBatch;

public class LineBullets {
//	private List<Bullet> unBirthBullets;
	private List<Bullet> aliveBullets;
	private List<Bullet> deadBullets;
//	private List<Bullet> BirthdayBullets;
	
	private Random r = new Random();
	private int time = 0;
	
	private boolean repeat = true;
	
	private Position position;
	private float token;
	private float spaceTime = 0.5f;
	private float totleTime = 0f;
	private float speed = 100;
	private float theta = 0;
	private byte type = 0;
	
	public LineBullets(Position position){
		this.position = position;
		aliveBullets = new LinkedList<Bullet>();
		deadBullets = new LinkedList<Bullet>();
		this.addBullets();
	}
	
	public void update(float deltaTime){
		totleTime += deltaTime;
		
		token += deltaTime;

		if(token > spaceTime){
			token = spaceTime;
			time ++;
		}
		
		if(token == spaceTime&& time < 2){
			addBullets();
			token = 0;
		}
		
		if(time >20 && repeat){
			time = 0;
		}
		
		Log.println("Update num="+aliveBullets.size());
		
		for(Bullet next: aliveBullets){
			if(next.isAlive()){
				next.update(deltaTime);
			}else{
				deadBullets.add(next);
			}
		}
		
		aliveBullets.removeAll(deadBullets);
		deadBullets.clear();
		
		Log.println("AliveNum="+ aliveBullets.size());
	}
	
	public void render(SpriteBatch spriteBatch) {
		Log.println("Render num="+aliveBullets.size());
		for(Bullet next: aliveBullets){
			next.render(spriteBatch);
		}
	}
	
	private void addBullets(){
		for (int i = 0; i < 360; i += 10) {
			double rad = 2 * Math.PI * ((double) (i) / 360) - Math.PI;
			Bullet bullet = new Bullet(position, (float) rad, r.nextInt(2));
			bullet.setShape(new ShapeListener(){

				@Override
				public void update(float deltaTime, float deltaTheta) {
				}

				@Override
				public float updateRadius(float a,float theta) {
//					if(!inited){
//						this.radius = this.a * theta;
//						this.radius = this.a / theta;
//						this.radius = (float) ((float) Math.sin(theta)*Math.sqrt(Math.abs(Math.cos(theta)))
//								/(Math.sin(theta)+7/5)-2*Math.sin(theta)+2);
//						this.radius = (float) (this.a*(1+ Math.cos(theta)));	// mei gui xian
//						this.radius = (float) (this.a* Math.cos(2*theta));		// yuan pan xian
//						this.radius = (float) (Math.sin(theta) + Math.pow(Math.sin(5*theta), 3))*a;	// lian hua xian
//						inited = true;
//						Log.println("caicai");
//					}
					return a;
				}

				@Override
				public float updateTheta(float livingTime, float theta) {
					return theta;
				}

				@Override
				public float updateA(float livingTime, float a) {
					return speed * livingTime;
				}
				
			});
			aliveBullets.add(bullet);
		}
		Log.println("Add Bullets");
	}
	
}
